from PyQt4.QtGui import QDesktopServices, QFileDialog, QLineEdit, QMenu, QMessageBox
from PyQt4.QtCore import QUrl, SIGNAL
import os

class DocumentSelector(QLineEdit):
    def __init__(self, parent=None):
        super(DocumentSelector, self).__init__(parent)
        
        self._document_url = None
        
        self._initCustomStyleSheets()
        self._initGui()
    
    def _initCustomStyleSheets(self):
        self._cssNormal = """
            QLineEdit {
                color: #000000;
                font-style: normal;
            }
        """
        
        self._cssError = """
            QLineEdit {
                color: #FF0000;
                font-style: italic;
            }
        """
        
    def _initGui(self):
        self.setReadOnly(True)
    
    def _viewDocument(self):
        QDesktopServices.openUrl(QUrl("file:///" + self._document_url))
    
    def _replaceDocument(self):
        self._removeDocument()
        
        if not self._document_url:
            self._addDocument()
        
    def _removeDocument(self):
        result = QMessageBox.warning(self, "Removal Warning", "Are you sure you would like to remove the current file?",
                                     QMessageBox.Yes|QMessageBox.No, QMessageBox.No)
        
        if result == QMessageBox.Yes:
            self.clear()
            self._document_url = None
    
    def _addDocument(self):
        filename = QFileDialog.getOpenFileName(None, "Select document...")
        
        if filename:
            self._document_url = str(filename)
            
            self._updateDisplay()
    
    def _updateDisplay(self):
        self.setToolTip(self._document_url)
        
        if os.path.exists(self._document_url):
            display_text = self._document_url.split("/")[-1]
            self.setStyleSheet(self._cssNormal)
        else:
            display_text = self._document_url.split("/")[-1] + " (MISSING)"
            self.setStyleSheet(self._cssError)
            
        self.setText(display_text)
        
        
    def getDocumentUrl(self):
        return self._document_url
    
    def setDocumentUrl(self, document_url):
        self._document_url = document_url
        self._updateDisplay()
        
    def contextMenuEvent(self, evt):
        menu = QMenu()
        
        if self._document_url:
            actViewDocument = menu.addAction("View document")
            actReplaceDocument = menu.addAction("Replace document")
            actRemoveDocument = menu.addAction("Remove document")
            
            if not os.path.exists(self._document_url):
                actViewDocument.setEnabled(False)
                
            self.connect(actViewDocument, SIGNAL("triggered()"), self._viewDocument)
            self.connect(actReplaceDocument, SIGNAL("triggered()"), self._replaceDocument)
            self.connect(actRemoveDocument, SIGNAL("triggered()"), self._removeDocument)
            
        else:
            actAddDocument = menu.addAction("Select document")
            
            self.connect(actAddDocument, SIGNAL("triggered()"), self._addDocument)
            
        menu.exec_(evt.globalPos())

def main():
    from PyQt4.QtGui import QApplication, QHBoxLayout, QWidget
    from ftdb import env
    import sys
    
    env.initFTDBEnvironment()
    
    app = QApplication(sys.argv)
    
    document_url = "C:/Users/Kevin/Pictures/PetSmart Class Graduations/IMG_0790.PNG"
    panel = QWidget()
    lm = QHBoxLayout()
    document_selector = DocumentSelector()
    
    document_selector.setDocumentUrl(document_url)
    
    lm.addWidget(document_selector)
    
    panel.setLayout(lm)
    panel.show()
    panel.raise_()
    
    app.exec_()

if __name__ == "__main__":
    main()